package com.sunning.flutter_oupay;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.ccb.ccbnetpay.message.CcbPayResultListener;
import com.ccb.ccbnetpay.platform.CcbPayPlatform;
import com.ccb.ccbnetpay.platform.Platform;
import com.ccb.ccbnetpay.util.IPUtil;

import com.unionpay.UPPayAssistEx;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.crypto.SecureUtil;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class OupayCCBPay {
    private static final String TAG = "OupayCCBPay";
    private final Activity g_activity;
    private MethodChannel.Result g_callback;
    public OupayCCBPay(Activity activity){
        this.g_activity = activity;
    }


    //    //检测APP是否安装
    public boolean checkInstallApp(final String appId){
        String packageName="com.ccb.loongpay";//龙支付的包名
        PackageManager pm = this.g_activity.getPackageManager();
        Intent checkIntent = pm.getLaunchIntentForPackage(packageName);
        return null != checkIntent;
    }

    // 调起支付
    public void startPay(String payInfo,boolean isSandbox, final MethodChannel.Result callback){
        this.g_callback = callback;

        //MAC为MD5校验值
        payInfo+="&MAC="+SecureUtil.md5(payInfo);


        Log.d("OUPAY_CCB",payInfo);
        /*
         * 支付控件返回字符串:success、fail、cancel 分别代表支付成功，支付失败，支付取消
         */
        Map<String, String>  resultMap = new HashMap<String,String>();
        Platform ccbPayPlatform = new CcbPayPlatform
                .Builder()
                .setActivity(g_activity)
                // 支付回调
                .setListener(new CcbPayResultListener() {
                    @Override
                    public void onSuccess(Map<String, String> map) {
                        String payResult =map.get("SUCCESS");

                        //Y是成功 N是错误
                        if (payResult.equalsIgnoreCase("Y")) {
                            resultMap.put("pay_result", payResult);
                            g_callback.success(resultMap);
                        }
                    }

                    @Override
                    public void onFailed(String s) {
                        Log.d(TAG, "接口请求失败 --");
                    }
                })
                .setParams(payInfo)
                // 支付模式  建行APP 支付
                .setPayStyle(Platform.PayStyle.APP_OR_H5_PAY)
                .build();
        ccbPayPlatform.pay();
    }


//    public boolean onActivityResult(int requestCode, int resultCode, Intent intent) {
//        if (null == intent) {
//            return false;
//        }
//        return true;
//    }
}

